package com.wuyou.simple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReferenceArray;

/**
 * 2325. 解密消息
 */
public class DecodeMessage {

    public static void main(String[] args) {
        String key = "the quick brown fox jumps over the lazy dog";
        String message = "vkbs bs t suepuv";
//        "eljuxhpwnyrdgtqkviszcfmabo"
//        "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
//        System.out.println(solution_2(key, message));


        Long aLong = new Long(31998);
        Long bLong = new Long(31998);
        System.out.println(aLong == bLong);
        System.out.println(aLong.equals(bLong));
        System.out.println(aLong.longValue() == bLong.longValue());
    }

    /**
     * 定长数据用数组比 StringBuilder 快
     *
     * @param key
     * @param message
     * @return
     */
    public static String solution_2(String key, String message) {
        int[] charsTable = new int[26];
        char[] keyChars = key.toCharArray();
        char[] mesChars = message.toCharArray();
        char[] res = new char[message.length()];
        char index = 1;
        for (int i = 0; i < keyChars.length; i++) {
            char key1 = keyChars[i];
            if (key1 != ' ' && charsTable[key1 - 'a'] == 0) {
                charsTable[key1 - 'a'] = index++;
                if (index == 27) break;
            }
        }
        for (int i = 0; i < mesChars.length; i++) {
            if (mesChars[i] == ' ') {
                res[i] = mesChars[i];
            } else {
                res[i] = (char) (charsTable[mesChars[i] - 'a'] - 1 + 'a');
            }
        }
        return new String(res);
    }

    public static String solution_1(String key, String message) {
        Map<Character, Character> compareMap = new HashMap<>();

        char c = 'a';
        char[] keyChars = key.toCharArray();
        for (int i = 0; i < keyChars.length; i++) {
            char key1 = keyChars[i];
            if (key1 != ' ' && compareMap.get(key1) == null) {
                compareMap.put(key1, c++);
            }
        }

        StringBuilder sb = new StringBuilder();
        char[] mesChars = message.toCharArray();
        for (int i = 0; i < mesChars.length; i++) {
            char c1 = mesChars[i];
            if (c1 == ' ') {
                sb.append(c1);
            } else {
                sb.append(compareMap.get(c1));
            }
        }
        return sb.toString();
    }
}
